import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import Login from '../components/Login.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'Login', component: Login },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: () => import('../components/Home.vue'),
    children: [
      { path: '/welcome', name: 'Welcom', component: () => import('../components/Welcome.vue') },
      { path: '/users', name: 'Users', component: () => import('../components/users/Users.vue') },
      { path: '/rights', name: 'Rights', component: () => import('../components/power/Rights.vue') },
      { path: '/roles', name: 'Roles', component: () => import('../components/power/Roles.vue') },
      { path: '/orders', name: 'Orders', component: () => import('../components/orders/Orders.vue') },
      { path: '/categories', name: 'Categories', component: () => import('../components/goods/Categories.vue') },
      { path: '/goods', name: 'Goods', component: () => import('../components/goods/Goods.vue') },
      { path: '/goods/add', name: 'AddGoods', component: () => import('../components/goods/AddGoods.vue') },
      { path: '/params', name: 'Params', component: () => import('../components/goods/Params.vue') },
      { path: '/reports', name: 'Reports', component: () => import('../components/reports/Reports.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由导航守卫
router.beforeEach((to, from, next) => {
  // to === 将要访问的路径， from === 从哪个路由来， next === 放行 next（）放行 next('/login' 强制跳转)
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
